package com.ybybh.gamecenter.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.Random;

@Controller
public class GuessNumberGameController {
    private static int guessCount = 0;
    private static String resultMessage = "输入一个1-100之间的数字";
    //生成一个随即工具类
    private static Random random = new Random();
    private static int randomNumber = 0;

    private static boolean isFinish = false;

    @RequestMapping("/gnplay")
    public ModelAndView toGnPlay(int guess) {
        if (isFinish) {
            resultMessage = "本局游戏结束<a href='/gn'>再来一局</a>";
        } else if (guessCount == 5) {
            resultMessage = "本局猜错次数已达上限你输了<a href='/gn'>再来一局</a>";
            isFinish = true;
        } else {
            guessCount++;
            if (guess > randomNumber) {
                resultMessage = "猜大了";
            } else if (guess < randomNumber) {
                resultMessage = "猜小了";
            } else {
                resultMessage = "恭喜你猜对了<a href='/gn'>再来一局</a>";
                isFinish = true;
            }
        }
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("guessCount", guessCount);
        modelAndView.addObject("resultMessage", resultMessage);
        modelAndView.setViewName("guessnumber");
        return modelAndView;
    }
        @RequestMapping("/gn")
        public ModelAndView toGnHtml () {
            // 游戏逻辑
            randomNumber = random.nextInt(100) + 1;
            System.out.println(randomNumber);
            guessCount = 0;
            resultMessage = "输入一个1-100之间的数字";
            isFinish = false;

            ModelAndView modelAndView = new ModelAndView();
            modelAndView.addObject("guessCount", guessCount);
            modelAndView.addObject("resultMessage", resultMessage);
            modelAndView.setViewName("guessnumber");
            return modelAndView;
        }
    }
